package com.jiaquyun.jcyx.main.mine

import android.app.Activity
import android.content.Intent
import android.webkit.WebSettings
import android.webkit.WebViewClient
import androidx.lifecycle.Observer
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.jetpack.mode.ArticleViewModel
import com.jiaquyun.jcyx.utils.WebViewUtils
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_mine_rich_text.*

class MineRichTextActivity : OwnerViewModeActivity<ArticleViewModel>() {

    companion object {
        private const val EXTRA_KEY_TITLE = "EXTRA_KEY_TITLE"
        private const val EXTRA_KEY_ID = "EXTRA_KEY_NAME"
        private const val EXTRA_KEY_URL = "EXTRA_KEY_URL"
        fun openId(activity: Activity, title: String?, id: Int?) {
            val intent: Intent = Intent(activity, MineRichTextActivity::class.java).apply {
                putExtra(EXTRA_KEY_TITLE, title)
                putExtra(EXTRA_KEY_ID, id)
            }
            activity.startActivity(intent)
        }

        fun openUrl(activity: Activity, title: String?, url: String?) {
            val intent: Intent = Intent(activity, MineRichTextActivity::class.java).apply {
                putExtra(EXTRA_KEY_TITLE, title)
                putExtra(EXTRA_KEY_URL, url)
            }
            activity.startActivity(intent)
        }
    }

    override fun viewModel(): ArticleViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_rich_text

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, view = flRichTextBar)
    }

    override fun initView() {

        ivAppBack.setOnClickListener { finish() }

        intent?.getStringExtra(EXTRA_KEY_TITLE)?.let {
            tvRichTextTitle.text = it
        }

        wvRichTextWeb.webViewClient = WebViewClient()
        wvRichTextWeb.settings.apply {
            //支持缩放，默认为true。
            setSupportZoom(false)
            //调整图片至适合webview的大小
//            useWideViewPort = true
            // 缩放至屏幕的大小
            loadWithOverviewMode = true
            //设置默认编码
            defaultTextEncodingName = "utf-8"
            //设置自动加载图片
            loadsImagesAutomatically = true
            //允许访问文件
            allowFileAccess = true
            //开启javascript
            javaScriptEnabled = true
            //支持通过JS打开新窗口
            javaScriptCanOpenWindowsAutomatically = true
            //支持内容重新布局
            layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
            //关闭webview中缓存
            cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
        }
    }

    override fun initData() {
        val id = intent?.getIntExtra(EXTRA_KEY_ID, 0) ?: 0
        if (id != 0) {
            viewModel.articleDetails(id)
        }
        intent?.getStringExtra(EXTRA_KEY_URL)?.let {
            wvRichTextWeb.loadUrl(it)
        }
    }

    override fun initViewMode() {
        viewModel.articleDetailsLiveData.observe(this, Observer { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS) {
                return@Observer
            }
            val content = response.data?.data?.content
                ?: return@Observer

            WebViewUtils.loadFullScreenHtml(wvRichTextWeb, content)
        })
    }

}